var input=null;
var span=null;


$(function (){
    $("#back").click(function () {
        window.history.back(-1);
    });
    $("#drugName").blur(function (){
        var drugName = $("#drugName").val();
        if (drugName != null && drugName.trim() != ""){
            $.ajax({
                type: "GET",
                url: "writereport/drugnum",
                data: "drugName=" + drugName,
                dataType: "json",
                success:function (result){
                    var options=null;
                    if (result != null) {
                        var options1 = "<option>---请选择---</option>"
                        for (var i = 0; i < result.length; i++) {
                            options += "<option value="+result[i]+">"+result[i]+"</option>"
                        }
                        $("#drugNum").html(options1+options);
                    }
                },
                error: function (result) {
                    alert("drugName:查询药品名称失败")
                }
            })
        }
    });
    $("#drugNum").click(function (){
        var drugNameVal = $("#drugName").val();
        var drugNumVal = $("#drugNum").val();
        if (drugNumVal != null && drugNumVal.trim() != ""){
            $.ajax({
                type: "GET",
                url: "writereport/drug",
                data: {"drugName": drugNameVal, "drugNum": drugNumVal},
                dataType: "json",
                success:function (result){
                    if (result != null) {
                        input = "<input type='text' id='price' value="+ result.drugPrice+
                                "><input type='hidden' id='drugId' value="+ result.drugId+">"
                        $("#drugPrice").html(input);
                    }
                },
                error: function (result) {
                    alert("drugPrice:查询单价失败")
                }
            })
        }
    });

    var drugIdList = [];
    var drugNumberList = [];
    $("#add").click(function (){
        var drugNameVal = $("#drugName").val();
        var drugNumVal = $("#drugNum").val();
        var priceVal = $("#price").val();
        var drugIdVal = $("#drugId").val();
        var drugNumberVal = $("#drugNumber").val();
        if (drugNameVal == null && drugNameVal.trim() == ""){
            alert("药品名称不能为空")
        }else if (drugNumVal == "---请选择---" || priceVal.length == 0){
            alert("请选择药品编号")
        }else if (drugNumberVal.length == 0){
            alert("药品数量不能为空")
        }else {
            drugNumberList.push(parseInt(drugNumberVal));
            drugIdList.push(parseInt(drugIdVal));
            //alert(parseInt(drugNumberVal)==1)
            //alert("数量:"+drugNumberList+"Id:"+drugIdList)
            /*drugNumberList.push(drugNumberVal);
            drugIdList.push(drugIdVal);*/
            input = "<input type='text' value="+ drugNameVal+ "><input type='text' value="+ drugNumVal+
                "><input type='text' value="+priceVal+"><input type='text' name='drugNumber[]' value="+ drugNumberVal+
                "><input type='hidden' name='drugId[]' value="+ drugIdVal +"><br>"
            $("#addDrug").append(input);
        }
    });

    $("#submit").submit(function (){
        //alert("数量:"+drugNumberList+"Id:"+drugIdList);
        var record1 = $("#submit").serialize()+"&"+'drugNumberList='+drugNumberList +"&" +'drugIdList='+drugIdList;
        //alert(record1);
        $.ajax({
            type: "POST",
            url: "writereport/towritereport",
            data: record1,
            dataType: "json",
            success: function (result) {
                if (result == "success"){
                    alert("提交成功")
                }
            },
            error: function (result) {
                alert("提交失败")
            }
        });
        return false;
    });

    var doctorIdVal = $("#doctorId").val();
    $.ajax({
        type: "GET",
        url: "writereport/todDepartment",
        data: "doctorId="+doctorIdVal,
        dataType: "json",
        success: function (result) {
            $("#recordDepartment").val(result);
        },
        error: function (result) {
            alert("获得科室失败")
        }
    });
});
